//
//  CZServiceLogCell.m
//  TCProject
//
//  Created by admin on 16/1/25.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "CZServiceLogCell.h"
#import "ZDFeeCell.h"

@interface CZServiceLogCell()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *h1;
@property (weak, nonatomic) IBOutlet UILabel *h4;

@property (weak, nonatomic) IBOutlet UILabel *h2;

@property (weak, nonatomic) IBOutlet UIButton *editButton;
@property (weak, nonatomic) IBOutlet UILabel *h3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightMarginConstraint;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *h5;
@property (weak, nonatomic) IBOutlet UIButton *statusButton;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation CZServiceLogCell

- (void)awakeFromNib {
    UIView *containerView = self.contentView.subviews.firstObject;
    containerView.layer.shadowColor = [UIColor grayColor].CGColor;
    containerView.layer.shadowOpacity = 0.2;
    containerView.layer.shadowOffset = CGSizeMake(0, 2);
    self.tableView.rowHeight = 28.f;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDFeeCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    [super setSelected:selected animated:animated];
}

- (IBAction)editAction:(id)sender {
    if (_editOperation) {
        _editOperation();
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _log.typeCharges.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDFeeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.fee = _log.typeCharges[indexPath.row];
    return cell;
}

- (void)setLog:(ServiceLog *)log
{
    _log = log;
    
    [_statusButton setImage:[UIImage imageNamed:@"status_work1"] forState:UIControlStateNormal];
    switch (_log.status) {
        case 0:
            [_statusButton setTitle:@"待签收" forState:UIControlStateNormal];
            break;
        case 1:
            [_statusButton setTitle:@"待结算" forState:UIControlStateNormal];
            break;
        case 2:
            [_statusButton setTitle:@"已结算" forState:UIControlStateNormal];
            [_statusButton setImage:[UIImage imageNamed:@"status_work0"] forState:UIControlStateNormal];
            break;
    }
    
    _priceLabel.text = [NSString stringWithFormat:@"￥%.1f", _log.realsumPrice];
    [self.tableView reloadData];
    _heightConstraint.constant = self.tableView.contentSize.height;
    _h1.text = [NSString stringWithFormat:@"服务时间：%@", _log.startTime];
    _h2.text = [NSString stringWithFormat:@"服务方量：%.1f方", _log.volume];
    _h3.text = [NSString stringWithFormat:@"服务说明：%@", [BMUtils isEmptyString:_log.remarks] ? @"无" : _log.remarks];
    self.h4.text = [NSString stringWithFormat:@"服务距离：%@", [BMUtils isEmptyString:_log.distance] ?  @"无" : _log.distance];
    
    CGFloat total = _log.realsumPrice;
    for (TypeCharge *type in _log.typeCharges) {
        total -= type.charge;
    }
    
    self.h5.text = [NSString stringWithFormat:@"方量单价：%.1f元/方", total/_log.volume];
    
    if (_log.status == 0) {
        self.editButton.hidden = NO;
        self.rightMarginConstraint.priority = 800;
    } else {
        self.editButton.hidden = YES;
        self.rightMarginConstraint.priority = 700;
    }
}

@end
